package org.yjc.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.yjc.entity.Student;
import org.yjc.entity.Teacher;
import org.yjc.service.StudentService;
import org.yjc.service.TeacherService;

@Controller
public class LoginController {
	@Autowired
	private StudentService studentservice;
	@Autowired
	private TeacherService teacherservice;
	
	/***
	 * @return 进入登陆页面
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	
	/***
	 * 登陆功能
	 * @param userName 用户名
	 * @param passWord 密码
	 * @param xz 权限选择
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String userName,String passWord,String xz,HttpSession session) {
		//权限判断
		if(xz.equals("学生")) {
			Student studentlogin = studentservice.login(userName,passWord);
			if(studentlogin==null) {
				session.removeAttribute("student");
				return "redirect:login";
			}else {
				session.setAttribute("student", studentlogin);
				List<Student> studentlist=studentservice.findAll();
				return "redirect:student/studentmain";
			}
		}else if(xz.equals("教师")) {
			Teacher teacherlogin = teacherservice.login(userName,passWord);
			if(teacherlogin==null) {
				session.removeAttribute("teacher");
				return "redirect:login";
			}else {
				session.setAttribute("teacher",teacherlogin);
				List<Teacher> teacherlist = teacherservice.findAll();
				return "redirect:teacher/teachermain";
			}
		}
		return null;
	}
}
